package njust.csc.aecore

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import njust.csc.aecore.entity.*
import njust.csc.aecore.judge.GPTJudge
import njust.csc.aecore.judge.QWenJudge
import njust.csc.aecore.utils.PathUtils
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.StandardCharsets
import kotlin.io.path.Path
import kotlin.io.path.readLines


/**
 * @Author ZhouHang
 * @Date 24/6/4 周二
 * 将静态动态分析出来的数据处理成大模型需要的数据
 */

fun main() {
//    val blackAPKs = readApk("black", ApkType.BLACK)
//    val gambleAPKs = readApk("gamble", ApkType.GAMBLE)
//    val scamAPKs = readApk("scam", ApkType.SCAM)
//    val sexAPKs = readApk("sex", ApkType.SEX)
//    val whiteAPKs = readApk("white", ApkType.WHITE)
//    val qwen = QWenJudge()

    val gpt = GPTJudge()
//    val res = gpt.ask(
//        "{\"absolutePath\":\"N:\\\\Code\\\\PL\\\\StaticAnalysis\\\\ApkExplorer\\\\input\\\\userInput\\\\android_385531710409242.apk\",\"relativePath\":\"userInput\\\\android_385531710409242.apk\",\"name\":\"在线娱乐\",\"packageName\":\"com.pro.stocktradeandroid.zxyl\",\"size\":1338,\"versionInfo\":{\"versionCode\":102,\"versionName\":\"1.02\",\"minSdkVersion\":23,\"targetSdkVersion\":29,\"compileSdkVersion\":29},\"permissions\":[\"android.permission.INTERNET\"],\"v1SignatureInfo\":{\"name\":\"CERT.RSA\",\"signatureFileName\":\"META-INF/CERT.SF\",\"certification\":{\"subject\":\"CN=CN,OU=CN,O=CN,L=CN,ST=CN,C=CN\",\"signatureType\":\"SHA256withRSA\",\"md5\":\"B6 B8 DB 46 82 42 3B E9 71 E3 B4 6A 6A B1 53 58 \",\"sha1\":\"5F 44 D5 09 3C 6D A9 C7 43 00 97 67 6B 9E F6 5D 04 09 84 B5 \",\"sha256\":\"9A 1E 1A 62 E5 2E 0C BE 50 02 CA D9 33 6F 5A 4C 2F C6 FE 1E CA 20 4F DF C9 85 CE 6D 3F 65 C5 A2 \"}},\"v2SignatureInfo\":{\"index\":0,\"certification\":{\"subject\":\"CN=CN,OU=CN,O=CN,L=CN,ST=CN,C=CN\",\"signatureType\":\"SHA256withRSA\",\"md5\":\"B6 B8 DB 46 82 42 3B E9 71 E3 B4 6A 6A B1 53 58 \",\"sha1\":\"5F 44 D5 09 3C 6D A9 C7 43 00 97 67 6B 9E F6 5D 04 09 84 B5 \",\"sha256\":\"9A 1E 1A 62 E5 2E 0C BE 50 02 CA D9 33 6F 5A 4C 2F C6 FE 1E CA 20 4F DF C9 85 CE 6D 3F 65 C5 A2 \"}},\"screenContent\":\"3:36四全上自9信守4和  3:36四全国六军4自  3:37四会国9信守4和  3:37四会国9信守4和  3:37个国   3:37四会国9信守4和  3:37四会国9信守4和  3:37四会国9信守4和  \",\"time\":6.269}\n"
//    )

    val res = gpt.askVision(Path("N:\\Code\\PL\\StaticAnalysis\\ApkExplorer\\output\\screencaps\\com.o2j66w.fgyvrr"))
    println(res)

//    val apks: MutableList<APK> = mutableListOf()
//    apks.addAll(blackAPKs)
//    apks.addAll(gambleAPKs)
//    apks.addAll(scamAPKs)
//    apks.addAll(sexAPKs)
//    apks.addAll(whiteAPKs)
//    apks.shuffle()
}


fun readApk(relativePath: String, type: ApkType): MutableList<APK> {
    val apks: MutableList<APK> = mutableListOf()
    for (line in PathUtils.getBaseInfoPath(relativePath, false).readLines()) {
        val apk = Json.decodeFromString<APK>(line)
        apk.type = type
        apk.isBad = apk.type != ApkType.WHITE
        apks.add(apk)
    }
    return apks
}
